<?php

namespace App\Models\Customer;

use App\Models\Panx\Product;
use Illuminate\Database\Eloquent\Model;

/**
 * @deprecated
 * Class CustomerSkinItemLevel
 * @package App\Models\Customer
 *
 * @mixin \Illuminate\Database\Eloquent\Builder
 * @mixin \Illuminate\Database\Query\Builder
 */

class CustomerSkinItemLevel extends Model
{
    const WAX_SKIN_TYPE = 'CustomerRelateWaxSkin'; // wax测肤类型
    const HM_SKIN_TYPE = 'CustomerRelateHmSkin';   // hm测肤类型

    protected $table = 'customer_skin_item_levels';
    protected $guarded = [];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function customerSkinItemLevelType()
    {
        return $this->morphTo('customer_relate_skin', 'customer_relate_skin_type', 'customer_relate_skin_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function products()
    {
        return $this->belongsToMany(Product::class, 'customer_skin_item_level_pivot_product', 'customer_skin_item_level_id', 'product_id')
            ->withPivot('store_id');
    }

    /**
     * @param $customer_id
     * @param null $skin_type
     * @return mixed
     */
    public static function checkCustomerSkinItemLevel($customer_id, $skin_type = null)
    {
        $customer_has_skin_item_level = (new self)
            ->where(compact('customer_id'))
            ->when($skin_type, function($query) use($skin_type){
                return $query->where('customer_relate_skin_type', $skin_type);
            })
            ->exists();

        return $customer_has_skin_item_level;
    }

    /**
     * @param $skin_id
     * @param $customer_id
     * @return mixed
     */
    public static function getCustomerSkinItemLevel($skin_id, $customer_id)
    {
        return (new self)
            ->where(compact('customer_id'))
            ->find($skin_id);
    }

    /**
     * @param $skin_relate_skin_id
     * @param $skin_type
     * @param $customer_id
     * @return mixed
     */
    public static function getCustomerSkinItemLevelByType($skin_relate_skin_id, $skin_type, $customer_id)
    {
        return (new self)
            ->where(compact('customer_id'))
            ->where('customer_relate_skin_id', $skin_relate_skin_id)
            ->where('customer_relate_skin_type', $skin_type)
            ->first();
    }
}
